package cn.itsource.hrm.controller;

import cn.itsource.hrm.context.LoginContext;
import cn.itsource.hrm.domain.LoginUser;
import cn.itsource.hrm.dto.CourseAddParamDTO;
import cn.itsource.hrm.service.ICourseService;
import cn.itsource.hrm.domain.Course;
import cn.itsource.hrm.query.CourseQuery;
import cn.itsource.hrm.util.AjaxResult;
import cn.itsource.hrm.util.PageList;
import cn.itsource.hrm.vo.CrumbsVO;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 服务接口controller
 * @author yaosang
 * @since 2022-04-10
 */
@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    public ICourseService courseService;



    /**
     * 课程上线
     */
    @PostMapping("/onLineCourse/{courseId}")
    @PreAuthorize("hasAuthority('course:online')")
    public AjaxResult onLineCourse(@PathVariable("courseId")Long courseId){
        courseService.onLineCourse(courseId);
        return AjaxResult.me();
    }
    /**
     * 保存和修改公用的
     */
    @PutMapping
    public AjaxResult add(@RequestBody @Valid CourseAddParamDTO dto){
        courseService.add(dto);
        return AjaxResult.me();
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        courseService.deleteById(id);
        return AjaxResult.me();
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Course get(@PathVariable("id")Long id)
    {
        return courseService.selectById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public List<Course> list(){

        return courseService.selectList(null);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Course> json(@RequestBody CourseQuery query)
    {
        Page<Course> page = new Page<Course>(query.getPage(),query.getRows());
        page = courseService.selectPage(page);
        return new PageList<Course>(page.getTotal(),page.getRecords());
    }
}
